<template>
  <el-dialog
    :title="`${addForm.id?'编辑':'添加'}员工`"
    width="480px"
    :visible="addVisible"
    @close="closeDialog"
    @open="openDialog"
  >
    <!-- 表单接口 -->
    <div class="form-container">
      <el-form ref="addForm" :model="addForm" :rules="addFormRules" label-width="80px">
        <el-form-item label="员工姓名" prop="name">
          <el-input v-model="addForm.name" />
        </el-form-item>
        <el-form-item label="登录账号" prop="userName">
          <el-input v-model="addForm.userName" />
        </el-form-item>
        <el-form-item label="联系方式" prop="phonenumber">
          <el-input v-model="addForm.phonenumber" />
        </el-form-item>
        <el-form-item label="分配角色" prop="roleId">
          <el-select v-model="addForm.roleId" placeholder="请选择角色">
            <el-option
              v-for="item in roleList"
              :key="item.roleId"
              :label="item.roleName"
              :value="item.roleId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="员工状态" prop="status">
          <el-radio-group v-model="addForm.status">
            <el-radio :label="0">禁用</el-radio>
            <el-radio :label="1">启用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
      <el-button size="mini" type="primary" @click="confirmAdd">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
// 获取rolelist接口
import { getRoleListAPI } from '@/api/role'
import { createEmployeeAPI, getRoleDetailAPI, updateRoleAPI } from '@/api/employee'

export default {
  props: {
    // 通过父组件传入 控制弹框打开和关闭
    addVisible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      visible: false,
      // 表单对象
      addForm: {
        name: '',
        phonenumber: '',
        roleId: '',
        status: 1,
        userName: ''
      },
      // 规则对象
      addFormRules: {
        name: [
          { required: true, message: '请输入员工姓名', trigger: 'blur' }
        ],
        userName: [
          { required: true, message: '请输入登录账号', trigger: 'blur' }
        ],
        phonenumber: [
          { required: true, message: '请输入联系方式', trigger: 'blur' }
        ],
        roleId: [
          { required: true, message: '请分配角色', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择员工状态', trigger: 'blur' }
        ]
      },
      roleList: [] // 角色列表
    }
  },
  methods: {
    // 获取角色详情
    async getRoleDetail(id) {
      const res = await getRoleDetailAPI(id)
      // 回填数据
      this.addForm = res.data
    },
    openDialog() {
      this.getRoleList()
    },
    // 获取角色列表
    async getRoleList() {
      const res = await getRoleListAPI()
      this.roleList = res.data
    },
    closeDialog() {
      // 子传父的方式 把位于父组件中的addVisible设置为false
      this.$emit('close-dialog')
      // 4. 清除数据 校验痕迹
      this.addForm = {
        name: '',
        phonenumber: '',
        roleId: '',
        status: 1,
        userName: ''
      }
      this.$refs.addForm.resetFields()
    },
    // 确认提交
    confirmAdd() {
      // 1. 表单统一校验
      this.$refs.addForm.validate(async valid => {
        if (valid) {
          if (this.addForm.id) {
            // 更新接口(从详情中拿到的数据有可能有多余的字段)
            const { id, name, userName, roleId, status, phonenumber } = this.addForm
            await updateRoleAPI({
              id, name, userName, roleId, status, phonenumber
            })
          } else {
            // 2. 调用新增接口
            await createEmployeeAPI(this.addForm)
          }

          // 后续逻辑操作
          // 1. 提示用户
          this.$message({
            type: 'success',
            message: `${this.addForm.id ? '更新' : '新增'}员工成功`
          })
          // 2. 更新列表
          this.$emit('update-list')
          // 3. 关闭弹框
          this.closeDialog()
        }
      })
    }
  }
}
</script>
